﻿using GDGeek;
using MrPP.UX;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace MrPP.SerriedCabinet { 
    public class MenuManager : GDGeek.Singleton<MenuManager>
    {
        [SerializeField]
        UnityEvent _inFixed;
        [SerializeField]
        UnityEvent _outFixed;

        [SerializeField]
        private GameObject _root;
      //  [SerializeField]
      //  private MenuRoot _left;
        [SerializeField]
        private Book _leftBook;


        [SerializeField]
        private BoxModel _model;
      
        private void refresh() {
            refresh(_model.datas);
        }
        private void doBox(int last, int next) {
            _root.gameObject.SetActive(false);
            if (next == CabinetGroup.Instance.fix)
            {
                _inFixed?.Invoke();
            }
            else {
                _outFixed?.Invoke();
            }
            if(next != -1) {
                Box box = BoxRoot.Instance.manager.getBox(next);
                if (box != null) {
                    
                    _root.gameObject.SetActive(true);
                    this.doMount(box);
                }
            }
        }
        void Start()
        {
            _model.onRefresh += refresh;
            _model.onBox += doBox;
            _model.doLPage += delegate
            {
                refresh();
            }; 
            _model.doRPage += delegate
            {
                refresh();
            };
            _root.gameObject.SetActive(false);
            this._leftBook.onSelected += leftSelected;
            this._rightBook.onSelected += rightSelected;
        }

        private void rightSelected(int idx)
        {
            if (idx < left_.Count && idx >= 0)
            {
                SerriedCabinetPost.Instance.doBoxMenu(left_[idx]);
            }

        }

        private void leftSelected(int idx)
        {
            if (idx < right_.Count && idx >= 0) {
                SerriedCabinetPost.Instance.doBoxMenu(right_[idx]);
            }
        }

        void OnDestory() {

         
        }
    

        [SerializeField]
        private Book _rightBook;
     
        public void doMount(Box box)
        {  
            _root.gameObject.SetActive(true);
            this.transform.SetParent(box.cube.transform);
            this.transform.localRotation = Quaternion.identity;
            this.transform.localPosition = new Vector3(0, 0.1f, -0.51f);
        }
        private List<int> left_ = new List<int>();
        private List<int> right_ = new List<int>();
        internal void refresh(BoxModel.DataSyncList datas)
        {
            left_.Clear();
            right_.Clear();
            List<string> left = new List<string>();
            List<string> right = new List<string>();
            for (int i = 0; i < datas.Count; ++i) {
                if (datas[i].type == Box.Type.Left)
                {
                    left_.Add(i);
                    left.Add(datas[i].text);
                }
                else
                {
                    right_.Add(i);
                    right.Add(datas[i].text);
                }
            }
            this._leftBook.setItems(left);
            this._leftBook.page = _model.lPage;
            this._leftBook.refresh();
            this._rightBook.setItems(right);
            this._rightBook.page = _model.rPage;
            this._rightBook.refresh();

        }

        
    }
}